package com.itheima.demo;

import java.util.*;


public class Test_001 {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        Person p = new Person("张三丰", 68, "男");
        Person p1 = new Person("张翠山", 25, "男");
        Person p2 = new Person("殷素素", 20, "女");
        Person p3 = new Person("何太冲", 33, "男");
        Person p4 = new Person("班淑娴", 34, "女");
        Collections.addAll(list, p, p1, p2, p3, p4);
        //method01(list);
        //method02(list);
        //method04(list);
        //method03(list);
        method08(list);
    }

    private static void method03(List<Person> list) {
        //找到集合中年纪最小的学生，并打印
        Collections.sort(list, (o1, o2) -> o1.getAge() - o2.getAge());
        list.remove(0);
        System.out.println(list);
    }
    private static void method08(List<Person> list) {
        //找到集合中年纪最小的学生，并打印
        Iterator<Person> iterator = list.iterator();
        int minAge = list.get(0).getAge();
        Person minPerson = null;
        while (iterator.hasNext()){
            Person per = iterator.next();
            if(minAge>per.getAge()){
                minAge = per.getAge();
                minPerson = per;
            }
        }
        list.remove(minPerson);
        System.out.println(list);
    }

    //删除集合中岁数大于 30 的对象
    private static void method04(List<Person> list) {
        //list.removeIf(person -> person.getAge() > 30);
        //System.out.println(list);
        list.removeIf(person -> person.getAge()>30);
    }

    //找到集合中年纪最小的学生，并打印
    //从控制台录入一个学生字符串，
    // 在集合中查找该字符串对应的学生对象是否存在（姓名，年龄，性别都相同才能判断存在），录入格式为：
    private static void method02(List<Person> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入性别:");
        String sex = sc.next();
        /*for (int i = 0; i < list.size(); i++) {
            Person per = list.get(i);
              if (per.getName().equals(name) && per.getAge() == age && per.getSex().equals(sex)) {
                System.out.println(name + "信息存在!");
                return;
            }
        }
        System.out.println("信息不存在!");*/
        Person p = new Person(name, age, sex);
        Iterator<Person> it = list.iterator();
        while (it.hasNext()) {
            Person person01 = it.next();
            if (p.toString().equals(person01.toString())) {
                System.out.println(name + "信息存在!");
                return;
            }
        }
        System.out.println("信息不存在!");
    }

    //使用迭代器遍历
    private static void method01(List<Person> list) {
        Iterator<Person> it = list.iterator();
        while (it.hasNext()) {
            Person s = it.next();
            System.out.println(s);
        }
    }
}

